//
//  AppDelegate.m
//  FaceDiscernmentDemo
//
//  Created by mac on 2019/2/20.
//  Copyright © 2019年 mac. All rights reserved.
//

#import "AppDelegate.h"


@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    NSString* licensePath = [[NSBundle mainBundle] pathForResource:FACE_LICENSE_NAME ofType:FACE_LICENSE_SUFFIX];
    NSLog(@"license文件路径 == %@",licensePath);
    //设置鉴权
    [[FaceSDKManager sharedInstance] setLicenseID:FACE_LICENSE_ID andLocalLicenceFile:licensePath];
    NSLog(@"鉴权成功canWork = %d",[[FaceSDKManager sharedInstance] canWork]);
    NSLog(@"获取当前版本version = %@",[[FaceVerifier sharedInstance] getVersion]);
    
    [self getAccessTokenWithAK:FACE_API_KEY SK:FACE_SECRET_KEY];
    
    // Override point for customization after application launch.
    return YES;
}

/**
 * APP 启动的时候先获取token
 */
- (void)getAccessTokenWithAK:(NSString *)ak SK:(NSString *)sk {
    __weak typeof(self) weakSelf = self;
    NSTimeInterval start = [[NSDate date] timeIntervalSince1970];
    NSLog(@"start = %f",start);
    [WebConnect webPOSTConnectWithWithStrUrl:Baidu_getTokenURL pramaters:@{@"grant_type":@"client_credentials",@"client_id":ak,@"client_secret":sk} success:^(id responseObject) {
//        NSLog(@"responseObject == %@",responseObject);
        //存储token信息
        [FaceAppToolHandle shareToolHandle].accessToken = [NSString stringWithFormat:@"%@",responseObject[@"access_token"]];;
    } failure:^(id failure) {
        NSLog(@"错误=%@",failure);
    }];
//     postDataWithPath:ACCESS_TOEKN_URL parameters:@{@"grant_type":@"client_credentials",@"client_id":ak,@"client_secret":sk} completion:^(NSError *error, id resultObject) {
//        if (error == nil) {
//            NSTimeInterval end = [[NSDate date] timeIntervalSince1970];
//            NSLog(@"end = %f",end);
//            NSLog(@"Token = %f",end - start);
//            NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:resultObject options:NSJSONReadingAllowFragments error:nil];
//            weakSelf.accessToken = dict[@"access_token"];
//            NSLog(@"%@",dict[@"access_token"]);
//        }
//    }];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
